package com.sqx.modules.timedtask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.timedtask.dao.RiderLocationDao;
import com.sqx.modules.timedtask.entity.RiderLocation;
import com.sqx.modules.timedtask.service.RiderLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

@Service
public class RiderLocationServiceImpl extends ServiceImpl<RiderLocationDao, RiderLocation> implements RiderLocationService {

    @Autowired
    private RiderLocationDao riderLocationDao;
    @Autowired
    private CommonInfoService commonInfoService;

    @Transactional
    @Override
    public Result riderLocation(Long userId, Double lng, Double lat, String province, String city, String district) {
        RiderLocation riderLocation1 = baseMapper.selectOne(new QueryWrapper<RiderLocation>().eq("user_id", userId));
        if(riderLocation1==null){
            RiderLocation riderLocation2 = new RiderLocation();
            riderLocation2.setUserId(userId);
            riderLocation2.setLng(lng);
            riderLocation2.setLat(lat);
            riderLocation2.setProvince(province);
            riderLocation2.setCity(city);
            riderLocation2.setDistrict(district);
            baseMapper.insert(riderLocation2);
            return Result.success();
        }
        riderLocation1.setLng(lng);
        riderLocation1.setLat(lat);
        riderLocation1.setProvince(province);
        riderLocation1.setCity(city);
        riderLocation1.setDistrict(district);
        baseMapper.updateById(riderLocation1);
        return Result.success();
    }

    @Override
    public Result selectRiderLocation(Long riderUserId, Double lng, Double lat) {
        HashMap hashMap = new HashMap();
        //查看骑手即时位置
        RiderLocation riderLocation = baseMapper.selectOne(new QueryWrapper<RiderLocation>().eq("user_id", riderUserId));
        //计算骑手到用户的距离
        Double aDouble = riderLocationDao.calculateDistance(lng, lat, riderLocation.getLng(), riderLocation.getLat());
        //计算预计送达时间
        CommonInfo one = commonInfoService.findOne(301);
        Double a = aDouble/Double.valueOf(one.getValue());
        Integer integer = Integer.valueOf(a.intValue());

        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, +integer);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String mDateTime=formatter.format(c.getTime());

        hashMap.put("riderLocation", riderLocation);
        hashMap.put("aDouble", aDouble);
        hashMap.put("mDateTime", mDateTime);
        return Result.success().put("data", hashMap);
    }

    @Override
    public Result selectAllRiderLocation(String province, String city, String district) {
        List<RiderLocation> riderLocations = baseMapper.selectList(new QueryWrapper<RiderLocation>()
                .eq(province!=null && province.isEmpty() && !province.equals(""),"province", province)
                .eq(city!=null && city.isEmpty() && !city.equals(""), "city", city)
                .eq(district!=null && district.isEmpty() && !district.equals(""), "district", district));
        return Result.success().put("data", riderLocations);
    }


}
